package sample.engine;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.dialogs.SearchPattern;
import org.eclipse.ui.texteditor.ITextEditor;

import sample.MySearchPattern;
import sample.dialogs.JavaTypeNameMatch;
import sample.util.JavaTypeInfoUtil;
/**
 *
 * @author CuiKun cuikunbj@cn.ibm.com
 *
 */
public class JavaTypeItemsFilter {

	private IEditorPart editorPart;

//	private SearchPattern searchPattern;
	
	private MySearchPattern searchPattern;

	public JavaTypeItemsFilter(String pattern) {
		super();
		searchPattern = new MySearchPattern();
		searchPattern.setPattern(pattern);
		searchPattern.setEndText(JavaTypeInfoUtil.substringAfterLast(pattern, "/"));
	}

	public boolean isConsistentItem(Object item) {
		return true;
	}

	public String getPattern() {
		return searchPattern.getPattern();
	}

	public boolean equalsFilter(JavaTypeItemsFilter newFilter) {
		if (newFilter != null) {
			return newFilter.getSearchPattern().equals(this.searchPattern);
		}
		return false;
	}

	public boolean isSubFilter(JavaTypeItemsFilter itemsFilter) {
		if (itemsFilter != null) {
			return this.searchPattern.isSubPattern(itemsFilter
					.getSearchPattern());
		}
		return false;
	}

	public SearchPattern getSearchPattern() {
		return searchPattern;
	}

	/**
	 * add author:yyu date:2011-4-21
	 */
	public ITextSelection getSelection(){
        ISelectionProvider selectionProvider = getSelectionProvider();
        if (selectionProvider == null) {
            return null;
        }
        ISelection selection = selectionProvider.getSelection();
        if (selection instanceof ITextSelection) {
            return (ITextSelection) selection;
        }
        return null;
    }

	/**
	 * add author:yyu date:2011-4-21
	 */
    public String getSelectedText(){
        ITextSelection selection = getSelection();
        if(selection == null){
            return null;
        }
        String selectedText = selection.getText();
        if(selectedText != null && selectedText.length() > 0) {
            return selectedText;
        }
        return null;
    }

    /**
	 * add author:yyu date:2011-4-21
	 */
    public ISelectionProvider getSelectionProvider() {
        if (editorPart == null) {
            return null;
        }
        if (editorPart instanceof ITextEditor) {
            return ((ITextEditor) editorPart).getSelectionProvider();
        }
        if (editorPart instanceof ISelectionProvider) {
            return (ISelectionProvider) editorPart;
        }
        Object adapter = editorPart.getAdapter(ISelectionProvider.class);
        if(adapter instanceof ISelectionProvider){
            return (ISelectionProvider) adapter;
        }
        return null;
    }

	public boolean matchItem(Object item) {
		if (!(item instanceof JavaTypeNameMatch)) {
			return false;
		}
		JavaTypeNameMatch type = (JavaTypeNameMatch) item;
		return matches(type.getAuthorName());
	}

	public boolean matches(String text){
		return true;
	}
	
	public boolean matches(IFile file) {
//		if (text == null || "".equals(text)) {
//			return false;
//		}
		if(file.getName().startsWith(searchPattern.getEndText())){
			return file.getFullPath().toString().indexOf(searchPattern.getPattern()) != -1;
		}
		return false;
//		if(searchPattern.getEndText().matches(text)){
//			return text.indexOf(searchPattern.getPattern()) != -1;
//		} else {
//			return false;
//		}
		
//		return text.indexOf(searchPattern.getPattern()) != -1;
//		return searchPattern.matches(text);
	}
	
	public static void main(String[] args) {
		
	}

}
